package leetcode.recursive;

import java.util.ArrayList;
import java.util.List;

//77. 组合
public class Combine {
    //DFS 经典
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> combine(int n, int k) {
        dfs(1,n,k);
        return res;
    }

    private void dfs(int start, int n, int k) {
        if (k==0){
            res.add(new ArrayList<>(path));
            return;
        }
        //n-k+1：n-i>=k-1  当所剩的数字少于k个的时候，不满足条件，直接退出
        for (int i = start;i<=n-k+1;i++){
            path.add(i);
            dfs(i+1,n,k-1);
            path.remove(path.size()-1);
        }
    }
}
